package com.aneliya.mediator;

import java.awt.Dimension;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.ArrayList;

import javax.swing.SwingUtilities;

import com.aneliya.command.chatWindow.CloseChatWindowCommand;
import com.aneliya.command.chatWindow.MessageSend;
import com.aneliya.framework.mvc.Facade;
import com.aneliya.framework.mvc.controller.MVCEvent;
import com.aneliya.framework.mvc.view.Mediator;
import com.aneliya.model.Message;
import com.aneliya.view.ChatWindow;
import com.aneliya.view.component.input.TextInputEvent;
import com.aneliya.view.component.input.TextInputListener;

public class ChatWindowMediator extends Mediator {
	public static final String MESSAGE_SEND_EVENT = "messageSend";
	public static final String CLOSE_CHAT_WINDOW_EVENT = "closeChatWindow";
	private ChatWindow chatWindow;
	
	public ChatWindow getChatWindow() {
		return chatWindow;
	}

	public ChatWindowMediator() throws Exception {
		name = "ChatWindowMediator";
		facade = Facade.getInstance();
		
		initializeCommands();
	}

	private void initializeCommands()throws Exception {	
		facade.registerCommand(MESSAGE_SEND_EVENT, MessageSend.class);
		facade.registerCommand(CLOSE_CHAT_WINDOW_EVENT, CloseChatWindowCommand.class);
	}
	
	public void open() {
		SwingUtilities.invokeLater(new Runnable() {
			public void run() {
				try {
					initializeChatWindow();
					chatWindow.pack();
					chatWindow.setLocationRelativeTo(null);
					chatWindow.setVisible(true);
					chatWindow.setSize(new Dimension(500, 500));
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	public void close() {
		chatWindow.setVisible(false);
	}
	
	public void updateMessages(ArrayList<Message> messages) {
		chatWindow.setMessageVerticalBoxData(messages);
	}
	
	public void updateOnlineUsers(ArrayList<String> onlineUsers) {
		chatWindow.setUsersVerticalBoxData(onlineUsers);
	}
	
	private void initializeChatWindow() throws Exception {
		if (chatWindow == null) {
			chatWindow = new ChatWindow();
			initializeChatWindowListener();
		}
	}

	private void initializeChatWindowListener() {
		TextInputListener textInputListener = new TextInputListener() {
			public void sendButtonClicked(TextInputEvent e) {	
				try {
					dispatchMessageSend(e.getTextInputData());
				} catch (InstantiationException ex) {
					ex.printStackTrace();
				} catch (IllegalAccessException ex) {
					ex.printStackTrace();
				}
			}
			
		};
		chatWindow.addTextInputListener(textInputListener);
		
		WindowAdapter windowAdapter = new WindowAdapter() {
			public void windowClosing(WindowEvent e) {
				try {
					dispatchCloseChatWindowEvent();
				} catch (InstantiationException ex) {
					ex.printStackTrace();
				} catch (IllegalAccessException ex) {
					ex.printStackTrace();
				}
			}
		};
		chatWindow.addWindowListener(windowAdapter);
	}

	private void dispatchMessageSend(String message) throws InstantiationException, IllegalAccessException {
		facade.dispatchMVCEvent(new MVCEvent(this, MESSAGE_SEND_EVENT, message));
	}
	
	private void dispatchCloseChatWindowEvent() throws InstantiationException, IllegalAccessException {
		facade.dispatchMVCEvent(new MVCEvent(this, CLOSE_CHAT_WINDOW_EVENT, null));
	}
	
}
